<?xml version='1.0'?><!DOCTYPE html PUBLIC><html xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>jMock - Getting Started</title>
    <link href='/jmock.css' title='Navigation View' rel='stylesheet' type='text/css' media='screen' />
    <link href='/print.css' title='Print Preview' rel='alternate stylesheet' type='text/css' media='screen' />
    <link href='/print.css' rel='stylesheet' type='text/css' media='print' />
    <meta content='' http-equiv='Content-Type' />
  </head>

  <body>
    <div id='banner'>
      <a href='/index.html'><img src='/logo.gif' id='logo' alt='jMock' /></a>
    </div>

    

    
      <div class='Content2Column' id='center'>
        <div id='content'>
          <h1 class='FirstChild'>Getting Started</h1><p>This guide assumes you are familiar with unit-testing and <a href='http://junit.org/'>JUnit</a><span class='LinkFootnoteRef'><sup>1</sup></span>.</p><p>For a simple example we are going to test a publish/subscribe message
system. A Publisher sends objects to zero or more Subscribers.  We want to
test the Publisher, which involves testing its interactions with its
Subscribers.</p><p>The Subscriber interface looks like this:</p><div class='Source Java'>
<pre>interface Subscriber {
    void receive(Object message);
}</pre>
</div><p>We will test that a Publisher sends a message to a single registered
Subscriber. To test interactions between the Publisher and the Subscriber we
will use a mock Subscriber object.</p><p>First we must import the jMock classes, define our test fixture class and
define a test case method.</p><div class='Source Java'>
<pre>import org.jmock.*;

class PublisherTest extends MockObjectTestCase {
    public void testOneSubscriberReceivesAMessage() {
    }
}</pre>
</div><p>We will now write the body of the
<code>testOneSubscriberReceivesAMessage</code> method.</p><p>We first <i>set up</i> the context in which our test will execute. We
create a Publisher to test.  We create a mock Subscriber that should receive
the message.  We then register the Subscriber with the Publisher. Finally we
create a message object to publish.</p><div class='Source Java'>
<pre>Mock mockSubscriber = new Mock(Subscriber.class);
Publisher publisher = new Publisher();
publisher.add( (Subscriber)mockSubscriber.proxy() );

final Object message = new Object();</pre>
</div><p>Next we define <i>expectations</i> on the mock Subscriber that specify the
methods that we expect to be called upon it during the test run.  We expect
the receive method to be called with a single argument, the message that will
be sent.  The <code>same</code> method is defined in the MockObjectTestCase
class and specifies that we expect the argument to be the same object as
<var>message</var>. We don't need to specify what will be returned from the
receive method because it has a void return type.</p><div class='Source Java'>
<pre>mockSubscriber.expects(once()).method("receive").with( same(message) );</pre>
</div><p>We then <i>execute</i> the code that we want to test.</p><div class='Source Java'>
<pre>publisher.publish(message);</pre>
</div><p>Finally we <i>verify</i> that the mock Subscriber was called as expected. 
If we do not verify, our test will detect incorrect calls to the mock
Subscriber but not the complete lack of calls.</p><div class='Source Java'>
<pre>mockSubscriber.verify();</pre>
</div><p>Here is the complete test.</p><div class='Source Java'>
<pre>import org.jmock.*;

class PublisherTest extends MockObjectTestCase {
    public void testOneSubscriberReceivesAMessage() {
        // set up
        Mock mockSubscriber = new Mock(Subscriber.class);
        Publisher publisher = new Publisher();
        publisher.add((Subscriber) mockSubscriber.proxy());
        
        final Object message = new Object();
        
        // expectations
        mockSubscriber.expects(once()).method("receive").with( same(message) );
        
        // execute
        publisher.publish(message);
        
        // verify
        mockSubscriber.verify();
    }
}</pre>
</div><p>That concludes this quick introduction.  More advanced topics are covered
in <a href='docs.html#tutorials'>other tutorials</a><span class='LinkFootnoteRef'><sup>2</sup></span>.</p>
        <div class='LinkFootnotes'><p class='LinkFootnotesHeader'>Links:</p><p>1. <a href='http://junit.org/'>http://junit.org/</a></p><p>2. <a href='http://www.jmock.org/docs.html#tutorials'>http://www.jmock.org/docs.html#tutorials</a></p></div></div>
	    <div class='Meta'>
	      <p><a href='http://cvs.jmock.codehaus.org/jmock/website/content/getting-started.html'>Document History</a></p>
	    </div>
      </div>
    

    <div class='SidePanel' id='left'>
      <div class='MenuGroup'>
        <h1><a href='/download.html'>Software</a></h1>
        <ul>
          <li><a href='/download.html'>Download</a></li>
          <li><a href='/repository.html'>Anonymous CVS Access</a></li>
          <li><a href='/license.html'>Project License</a></li>
          <li><a href='/CHANGELOG'>Change Log</a></li>
        </ul>
      </div>

      <div class='MenuGroup'>
        <h1><a href='/docs.html'>Documentation...</a></h1>
        <ul>
          <li><a href='/getting-started.html'>Getting Started</a></li>
          <!-- <li><a href="">Frequently Asked Questions</a></li> -->
          <li><a href='/docs/javadoc/index.html' target='jmock-javadoc'>JavaDocs</a></li>
          <li><a href='http://www.mockobjects.com'>More about Mock Objects</a></li>
        </ul>
      </div>

      <div class='MenuGroup'>
        <h1>User Support</h1>
        <ul>
          <li><a href='/mailing-lists.html'>Mailing Lists</a></li>
          <li><a href='http://jira.codehaus.org/secure/BrowseProject.jspa?id=10336'>Issue Tracker</a></li>
          <li><a href='/news-rss2.xml'>News Feed (RSS 2.0)</a></li>
        </ul>
      </div>

      <div class='MenuGroup'>
        <h1><a href='/development.html'>Development...</a></h1>
        <ul>
          <li><a href='/how-to-contribute.html'>How to Contribute</a></li>
          <li><a href='/team.html'>Development Team</a></li>
        </ul>
      </div>

      <div class='MenuGroup'>
        <h1>Related Sites</h1>
        <ul>
          <li><a href='http://www.mockobjects.com'>Mock Objects</a></li>
          <li><a href='http://www.junit.org'>JUnit</a></li>
          <li><a href='http://www.nmock.org'>nMock</a></li>
          <li><a href='http://www.codehaus.org'>Project hosted by Codehaus</a></li>
        </ul>
      </div>
    </div>

    
  </body>
</html>